package com.controller;

import com.dto.UnloadGoodsDTO;
import com.entity.*;
import com.service.GoodsService;
import com.service.OrderService;
import com.service.UnloadGoodsService;
import com.util.CurrentUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/unloadgoods")
public class UnloadGoodsController {
    @Autowired
    private UnloadGoodsService unloadGoodsService;
    @Autowired
    private CurrentUser currentUser;
    @Autowired
    private GoodsService goodsService;

    @RequestMapping("/getAll/{pageIndex}/{pageSize}")
    public Result getAll(
            @PathVariable("pageIndex") Integer pageIndex,
            @PathVariable("pageSize") Integer pageSize,
            @RequestBody UnloadGoodsDTO unloadGoodsDTO
    ) {
//        CurrentUser currentUser =new CurrentUser();
        log.info("这是显示值：{}", unloadGoodsDTO);
        log.info("token={}", CurrentUser.token);
        log.info("这是名字{}", currentUser.getCurrentEname());
        log.info("这是eid{}", currentUser.getCurrentEid());
        log.info(String.valueOf(pageIndex), pageSize, unloadGoodsDTO);
        PageBean<UnloadGoodsDTO> pageBean = unloadGoodsService.getAll(pageIndex, pageSize, unloadGoodsDTO);
        log.info("返回的结果集为{}", pageBean);
        return Result.ok(pageBean);
    }

    @RequestMapping("add")
    public Result add(@RequestBody UnloadGoods unloadGoods) {
        log.info("申请入库，添加入库信息");
        unloadGoods.setEid(currentUser.getCurrentEid());//对接人
        List<GoodsEntity> goodsEntities = goodsService.findAll();
        for (GoodsEntity goodsEntity : goodsEntities) {
            //找到输入商品实体
            if (goodsEntity.getGname().equals(unloadGoods.getGname())) {
                unloadGoods.setGoodsUniqueNo(goodsEntity.getGoodsUniqueNo());
                unloadGoods.setBeforeNumber(goodsEntity.getSum());
                unloadGoods.setGid(goodsEntity.getGid());
            }
        }
        unloadGoods.setGno(32);
        unloadGoods.setIsApprove("待审核");
        unloadGoods.setIsunload("未入库");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        String format = sdf.format(new Date());
        unloadGoods.setCreationDate(format);
        log.info("添加部分信息后={}", unloadGoods);
        unloadGoodsService.addUnloadGoods(unloadGoods);
        return new Result<>(200, "添加成功", null);
    }

    @RequestMapping("/modify")
    public Result approve(@RequestBody UnloadGoodsDTO unloadGoodsDTO) {

        System.out.println("审核功能" + unloadGoodsDTO);
        unloadGoodsService.modify(unloadGoodsDTO);
        return Result.ok("审核成功！");
    }
}
